/**    
 * 文件名：FirstBillPamentManager.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2017年7月6日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.landseaserver.service.bill.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.common.exception.BusinessException;
import com.logic.landseaserver.common.LandeaConstants;
import com.logic.landseaserver.common.LandeaConstants.BillType;
import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.Arith;
import com.logic.landseaserver.common.util.LandSeaUtil;
import com.logic.landseaserver.domain.BillPayment;
import com.logic.landseaserver.persistence.read.BillReadMapper;
import com.logic.landseaserver.service.bill.PaymentManager;
import com.logic.landseaserver.ws.dto.BillDTO;
import com.logic.landseaserver.ws.dto.WeBankPayReqDTO;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：FirstBillPamentManager</br>
 * 类描述：月结账单类型</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月6日 下午8:10:04</br>
 * 
 * @version 1.0
 *
 */
@Service(LandeaConstants.BILLTYPE_PREFIX + BillType.MONTHLY_BILL)
public class MonthlyBillPamentManager implements PaymentManager
{
    private static final Logger LOG = LoggerFactory.getLogger(MonthlyBillPamentManager.class);
    
    @Autowired
    private BillReadMapper billReadMapper;
    
    /**
     * [描述]：和首期款一样</br>
     *
     * @param billPayment
     * @throws LandseaException 如果检查非法会报异常
     *
     */
    @Override
    public Double checkAmountMoney(BillPayment billPayment)
        throws LandseaException
    {
        Integer billId = billPayment.getBillId();
        
        try
        {
            // 查询ls_bill表
            BillDTO lsBillRecord = billReadMapper.selectByPrimaryKey(billId);
            if (null == lsBillRecord)
            {
                LOG.error("checkAmountMoney|no record in ls_bill table with billId=" + billId);
                throw new LandseaBusinessException(LandseaErrorMessageEnum.DEPOSIT_BILL_NOT_EXISTS_NULL);
            }
            
            // 应收款
            Double receivableMoney = lsBillRecord.getReceivableMoney();
            // 要转换成0.00保留两位小数
            receivableMoney = LandSeaUtil.get2Double(receivableMoney);
            // 已收款
            Double amountReceived = lsBillRecord.getAmountReceived();
            amountReceived = LandSeaUtil.get2Double(amountReceived);
            
            // 本次要支付的钱
            Double payMoney = billPayment.getMoney();
            payMoney = LandSeaUtil.get2Double(payMoney);
            
            // 要保证 总的支付不能多余总价格，你不能让租户吃亏
            if (receivableMoney.doubleValue() < Arith.add(payMoney, amountReceived))
            {
                throw LandseaException.createException(LandseaErrorMessageEnum.BILLPAYMENT_PAY_MONEY_ERROR);
            }
            
            return payMoney;
        }
        catch (LandseaException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            LOG.error("checkAmountMoney|check money failed.", e);
            throw LandseaException.createException(LandseaErrorMessageEnum.BILLPAYMENT_CHECK_PAYMONEY_ERROR, e);
        }
    }

    /**    
     * [描述]：</br>
     *
     * @param billId
     * @return
     * @throws LandseaException
     *
    */
    @Override
    public Integer getContractId(Integer billId)
        throws LandseaException
    {
        try
        {
            // 查询ls_bill表
            BillDTO lsBillRecord = billReadMapper.selectByPrimaryKey(billId);
            if (null == lsBillRecord)
            {
                LOG.error("getContractId|no record in ls_bill table with billId=" + billId);
                throw new LandseaBusinessException(LandseaErrorMessageEnum.DEPOSIT_BILL_NOT_EXISTS_NULL);
            }
            return lsBillRecord.getContractId();
        }
        catch (Exception e)
        {
            LOG.error("getContractId|query ls_bill error with billId=" + billId, e);
            throw LandseaException.createException(LandseaErrorMessageEnum.BILLPAYMENT_QUERY_BILL_ERROR, e);
        }
    }

    @Override
    public String getPrice(WeBankPayReqDTO payinfo)
        throws LandseaException, BusinessException
    {
        if(StringUtils.isEmpty(payinfo.getPrice())){
            throw LandseaException.createException(LandseaErrorMessageEnum.BILLPAYMENT_PRICE_NULL);
        }
        return payinfo.getPrice();
    }
}
